package com.cloud.provider.controller;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.provider.entity.User;
@RestController
@RequestMapping("/helloController")
public class HelloController {
	private final Logger logger=Logger.getLogger(getClass());
	@Autowired
	private DiscoveryClient client;
	
	@GetMapping("/sayHelloWorld")
	public String sayHelloWorld(){
		ServiceInstance localServiceInstance = client.getLocalServiceInstance();
		 String localServiceInfo="sayHelloWorld host:"+localServiceInstance.getHost()
			+",service_id:"+localServiceInstance.getServiceId()+" port:"+localServiceInstance.getPort();
		logger.info(localServiceInfo);
		
		return "Hello World,"+localServiceInfo;
	}
	@GetMapping("/hello")
	public String hello(){
		return "hello";
	}
	/**
	 * feign调用带参数实例
	 * @param name
	 * @return
	 */
	@GetMapping("/hello1")
	public String hello(@RequestParam String name){
		ServiceInstance localServiceInstance = client.getLocalServiceInstance();
		String localServiceInfo="sayHelloWorld host:"+localServiceInstance.getHost()
		+",service_id:"+localServiceInstance.getServiceId()+" port:"
				+localServiceInstance.getPort()+"----";
		return localServiceInfo+name;
	}
	/**
	 * feign调用返回VO示例
	 * @param name
	 * @param age
	 * @return
	 */
	@GetMapping("/hello2")
	public User hello2(@RequestHeader String name,@RequestHeader Integer age){
		return new User(name,age);
	}
	/**
	 * feign post调用入参VO示例
	 * @param user
	 * @return
	 */
	@PostMapping("/hello3")
	public String hello3(@RequestBody User user){
		return "hello "+user.getName()+","+user.getAge();
	}
}
